package pl.minim.unidefCMS.objects {
	import pl.minim.unidefCMS.UnidefManager;
	
	import com.googlecode.milib.util.MLArrayUtil;	

	/**
	 * @author Marek Brun
	 */
	public class UnidefPredefinedList extends AbstractUnidefObject {
		
		public var clazz:String;		private var selectedObjs:Array=[];

		public function UnidefPredefinedList(clazz:String, manager:UnidefManager) {
			this.clazz=clazz;
			this.manager=manager;
			
		}
		
		override public function getTypeName():String {
			return 'PredefinedList';
		}
		
		public function getObjectsToSelect():Array {
			return manager.getPredefinedList(clazz);
		}
		
		public function getSelectedObjs():Array {
			return selectedObjs.concat();
		}
		
		public function removeObj(unidef:UnidefClass):void {
			manager.removePredefinedObj(clazz, unidef);
		}
		
		public function addSelectedObj(unidef:AbstractUnidefObject):void {
			selectedObjs.push(unidef);
		}
		
		public function addSelectedObjByID(id:String):void {
			addSelectedObj(manager.getPredefined(clazz, id));
		}
		
		public function clearSelected():void {
			selectedObjs=[];
		}
		
		public function getIDs():Array {
			var arr:Array=[];
			for(var i:uint=0;i<selectedObjs.length;i++){
				arr.push(selectedObjs[i].id);
			}
			return arr;
		}
		
		override public function getDataAsTextPreview():String {
			return getIDs().join(',');
		}
		
		override public function toXML():XML {
			var xml:XML=<i/>;
			xml.appendChild(getIDs().join(','));
			return xml;
		}
		
		override public function reactToPredefinedObjectDelete(deletedObject:UnidefClass):void {
			MLArrayUtil.remove(selectedObjs, deletedObject);
		}
		
		override public function exportToRawData():* {
			var arr:Array=[];
			for(var i:uint=0;i<selectedObjs.length;i++){
				arr.push(AbstractUnidefObject(selectedObjs[i]).exportToRawData());
			}
			return arr;
		}
		
	}
}
/*

<item type="class" id="clients">
	<item type="txt" id="name" editLines="1"/>
	<item type="url" id="link"/>
</item>

*/